package controller;

import model.SysUsers;
import model.dto.SysUsersDto;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import service.SysUsersService;
import util.JwtUtil;
import util.MD5Util;
import util.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Validated
@Controller
@ResponseBody
@CrossOrigin//跨域
@RequestMapping("/users")
public class SysUsersController {

    @Resource
    private SysUsersService service;

    @RequestMapping("findAll.do")
    public Object findAll(SysUsersDto dto){
        //System.out.println(1 / 0);
        List<SysUsers> list = service.findAll(dto);
        int count = service.getCount(dto);
        return new Result<>("200","操作成功",list,count);
    }

    @RequestMapping("add.do")
    public Object add(HttpServletRequest request,SysUsers users){
        System.out.println(users);
//        String realPath = request.getServletContext().getRealPath("/static/images");
//        System.out.println(realPath);
//        String name = file.getOriginalFilename();
//        System.out.println(name);
//        File f = new File(realPath+File.separator+name);
//        try {
//            file.transferTo(f);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
        String salt = MD5Util.getSalt();
        String pwd = MD5Util.md5(users.getPassword()+salt);
        users.setSalt(salt);//设置盐值
        users.setPassword(pwd);//设置md5加盐都的密码
        //users.setHeadUrl(UUID.randomUUID().toString().substring(0,5)+name );//设置头像
        users.setCreatedUserId(1);

        int n = service.add(users);
        if(n>0){
            return new Result<>().ok();
        }else {
            return new Result<>().fail();
        }

    }

    @RequestMapping("/add2.do")
    public Object add2(SysUsers users){
        service.add(users);
        return new Result<>().ok();
    }

    @RequestMapping("upload.do")
    public Object upload(MultipartFile file, HttpServletRequest request){
        String realPath = request.getServletContext().getRealPath("/static/images");
        System.out.println(realPath);
        String name = file.getOriginalFilename();
        System.out.println(name);
        File f = new File(realPath+File.separator+name);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //更新数据库里头像
        return new Result<>().ok();

    }


    @RequestMapping("/login.do")
    public Object login(SysUsers users){
        SysUsers user = service.login(users);
        System.out.println(user);
        if(user==null){
            return new Result<>("0","用户名或密码错误");
        }else{
            String token = JwtUtil.createJwt(user);
            return new Result<>("0",token,user);
        }
    }

    @RequestMapping("/edit.do")
    public Object edit(SysUsers users){
        int n = service.edit(users);
        if(n>0){
            return new Result<>("200","修改成功");
        }else{
            return new Result<>("10002","修改失败");
        }
    }
    @RequestMapping("/del.do")
    public Object del(int id){
        int n = service.del(id);
        if(n>0){
            return new Result<>("200","修改成功");
        }else{
            return new Result<>("10002","修改失败");
        }
    }

    @RequestMapping("/delBatch.do")
    public Object delBatch(String ids){
        String[] id = ids.split(",");

        for(int i=0;i<id.length;i++){
            System.out.println(id[i]);
            service.del(Integer.parseInt(id[i]));
        }

        return new Result<>("200","修改成功");

    }

    @RequestMapping("/changeStatus.do")
    public Object changeStatus(int id){
        int n = service.changeStatus(id);
        if(n>0){
            return new Result<>("200","修改成功");
        }else{
            return new Result<>("10002","修改失败");
        }
    }



}
